package cn.iocoder.yudao.module.jky.controller.admin.orderdetail;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.jky.controller.admin.orderdetail.vo.*;
import cn.iocoder.yudao.module.jky.convert.orderdetail.OrderDetailConvert;
import cn.iocoder.yudao.module.jky.dal.dataobject.orderdetail.OrderDetailDO;
import cn.iocoder.yudao.module.jky.service.orderdetail.OrderDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 订单详情")
@RestController
@RequestMapping("/supplier/order-detail")
@Validated
public class OrderDetailController {

    @Resource
    private OrderDetailService orderDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建订单详情")
    @PreAuthorize("@ss.hasPermission('supplier:order-detail:create')")
    public CommonResult<Integer> createOrderDetail(@Valid @RequestBody OrderDetailCreateReqVO createReqVO) {
        return success(orderDetailService.createOrderDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单详情")
    @PreAuthorize("@ss.hasPermission('supplier:order-detail:update')")
    public CommonResult<Boolean> updateOrderDetail(@Valid @RequestBody OrderDetailUpdateReqVO updateReqVO) {
        orderDetailService.updateOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('supplier:order-detail:delete')")
    public CommonResult<Boolean> deleteOrderDetail(@RequestParam("id") Integer id) {
        orderDetailService.deleteOrderDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:order-detail:query')")
    public CommonResult<OrderDetailRespVO> getOrderDetail(@RequestParam("id") Integer id) {
        OrderDetailDO orderDetail = orderDetailService.getOrderDetail(id);
        return success(OrderDetailConvert.INSTANCE.convert(orderDetail));
    }

    @GetMapping("/list")
    @Operation(summary = "获得订单详情列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('supplier:order-detail:query')")
    public CommonResult<List<OrderDetailRespVO>> getOrderDetailList(@RequestParam("ids") Collection<Integer> ids) {
        List<OrderDetailDO> list = orderDetailService.getOrderDetailList(ids);
        return success(OrderDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单详情分页")
    @PreAuthorize("@ss.hasPermission('supplier:order-detail:query')")
    public CommonResult<PageResult<OrderDetailRespVO>> getOrderDetailPage(@Valid OrderDetailPageReqVO pageVO) {
        PageResult<OrderDetailDO> pageResult = orderDetailService.getOrderDetailPage(pageVO);
        return success(OrderDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单详情 Excel")
    @PreAuthorize("@ss.hasPermission('supplier:order-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderDetailExcel(@Valid OrderDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OrderDetailDO> list = orderDetailService.getOrderDetailList(exportReqVO);
        // 导出 Excel
        List<OrderDetailExcelVO> datas = OrderDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "订单详情.xls", "数据", OrderDetailExcelVO.class, datas);
    }

}
